package com.hanhanxiaochong.zhongxiangchuanmei.view.home.act;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.gyf.barlibrary.ImmersionBar;
import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.util.AppManager;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ResourcesUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 商店地图展示界面
 */
public class ShopMapActivity extends AppCompatActivity implements AMap.InfoWindowAdapter {

    @BindView(R.id.ll_leftview)
    RelativeLayout llLeftview;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.mapview)
    MapView mapView;

    private AMap aMap;
    private double lat;
    private double lng;
    private String shopName;
    private String shopAddress;
    private Unbinder unbinder;

    public static Intent createIntent(Context context, double lat, double lng, String shopName, String shopAddress) {
        Intent intent = new Intent(context, ShopMapActivity.class);
        intent.putExtra("lat", lat);
        intent.putExtra("lng", lng);
        intent.putExtra("shopName", shopName);
        intent.putExtra("shopAddress", shopAddress);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_map);
        unbinder = ButterKnife.bind(this);

        AppManager appManager = AppManager.getInstant();
        appManager.addActivity(this);
        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarColor(R.color.white)
                .statusBarDarkFont(true)
                .init(); //解决状态栏和布局重叠问题;

        //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState)，创建地图
        mapView.onCreate(savedInstanceState);

        Intent intent = getIntent();
        lat = intent.getDoubleExtra("lat", 0);
        lng = intent.getDoubleExtra("lng", 0);
        shopName = intent.getStringExtra("shopName");
        shopAddress = intent.getStringExtra("shopAddress");
        txtTitle.setText(shopName);

        //初始化地图控制器对象
        if (aMap == null) {
            aMap = mapView.getMap();
            //设置缩放级别
            aMap.moveCamera(CameraUpdateFactory.zoomTo(14));
        }
        initMap();
        setMarker();
    }

    @OnClick(R.id.ll_leftview)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_leftview:
                finish();
                break;
        }
    }

    private void setMarker() {
        MarkerOptions markerOption = new MarkerOptions();
        markerOption.position(new LatLng(lat, lng));
        markerOption.title(shopName).snippet(shopAddress);
        markerOption.draggable(false);//设置Marker可拖动
        markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                .decodeResource(getResources(), R.mipmap.ic_shop_location)));
        // 将Marker设置为贴地显示，可以双指下拉地图查看效果
        markerOption.setFlat(false);//设置marker平贴地图效果
        aMap.addMarker(markerOption);
    }

    private void initMap() {
        UiSettings mUiSettings = aMap.getUiSettings();
        mUiSettings.setCompassEnabled(true); //指南针可见
        aMap.setMyLocationEnabled(false);// 可触发定位并显示当前位置
        //设置自定义infowindow样式
        aMap.setInfoWindowAdapter(this);
        aMap.animateCamera(CameraUpdateFactory.changeLatLng(new LatLng(lat,
                lng)), 500, null);
    }

    @Override
    public View getInfoWindow(Marker marker) {
        View infoWindow = ResourcesUtils.inflate(R.layout.custom_infowindow);
        TextView shopName = infoWindow.findViewById(R.id.tv_shop_name);
        TextView shopAddress = infoWindow.findViewById(R.id.tv_shop_address);
        TextView navigate = infoWindow.findViewById(R.id.tv_go_to_navigate);
        shopName.setText(this.shopName);
        shopAddress.setText(this.shopAddress);
        navigate.setOnClickListener(v -> {
            Uri mUri = Uri.parse("geo:" + lat + "," + lng + "?q=" + this.shopAddress);
            Intent mIntent = new Intent(Intent.ACTION_VIEW, mUri);
            startActivity(mIntent);
        });
        marker.showInfoWindow();
        return infoWindow;
    }

    @Override
    public View getInfoContents(Marker marker) {
        marker.showInfoWindow();
        return null;
    }

    @Override
    protected void onDestroy() {
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        mapView.onDestroy();
        unbinder.unbind();
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        mapView.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mapView.onSaveInstanceState(outState);
    }
}
